<template>
  <Dialog v-model="dialogVisible" width="500px" title="添加端口规则" maxHeight="300px" @close="closeHandle">
    <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="80px" class="w-[400px]">
      <el-form-item label="协议" prop="protocol" required>
        <el-select v-model="formData.protocol" class="w-[100%]" clearable placeholder="请选择">
          <el-option label="tcp" value="tcp" />
           <el-option label="udp" value="udp" />
         <!-- <el-option label="tcpudp" value="tcpudp" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="端口" prop="port" required>
        <el-input v-model="formData.port" type="number" placeholder="请输入" maxlength="6">
        </el-input>
      </el-form-item>
      <el-form-item label="来源" prop="ip" required>
        <el-select v-model="formData.ip" class="w-[100%]" clearable placeholder="请选择">
          <el-option label="所有IP" value="ALL" />
          <el-option label="指定IP" value="DIY" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formData.ip == 'DIY'" label="指定IP" prop="diy_ip" required>
        <el-input v-model="formData.diy_ip" placeholder="请输入xx.xx.xx.xx" clearable maxlength="120" />
      </el-form-item>
      <el-form-item label="策略" prop="policy" required>
        <el-select v-model="formData.policy" class="w-[100%]" clearable placeholder="请选择">
          <el-option label="允许" value="accept" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="formData.note" placeholder="请输入" clearable maxlength="120" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeHandle">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="saveHandle(ruleFormRef)">提交</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch, defineEmits, reactive, onMounted } from 'vue'
import { setAddPortFireWallAPI } from "@/api/systemMonitor"
import { propTypes } from '@/utils/propTypes'
import { randomPassword } from '@/utils/index'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'confirm'])

const props = defineProps({
  modelValue: propTypes.bool.def(false),
})

//默认表单
const defaultFormData = {
  protocol: "tcp",
  ip: "",
  diy_ip: "",
  policy: "accept",
  note: "",
  port: "",
}

const loading = ref(false)
let formData = reactive({
  ...defaultFormData
})

const ruleFormRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  port: [
    { required: true, message: '此项必填', trigger: 'blur' },
    { type: 'number', message: '此项必须是一个数字' },
  ],
  ip: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  policy: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  protocol: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  diy_ip: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],

})
onMounted(() => {
})

//弹出框状态
const dialogVisible = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
})
//提交
const saveHandle = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      const data = JSON.parse(JSON.stringify(formData));
      if (data.ip != "ALL") {
        data.ip = data.diy_ip
      }
      delete data.diy_ip;
      setAddPortFireWallAPI({
        ...data,
      }).then(() => {
        ElMessage.success("添加成功")
        closeHandle()
        emit('confirm')
      }).finally(() => {
        loading.value = false;

      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//关闭
const closeHandle = () => {
  dialogVisible.value = false;
  //初始化数据
  Object.assign(formData, defaultFormData);
  emit('update:modelValue', dialogVisible)
}
</script>

<style lang="less" scoped>

</style>
